package com.jsmile.cloud.taskcenter.task.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jsmile.cloud.taskcenter.api.req.*;
import com.jsmile.cloud.taskcenter.task.TaskApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
@Api(tags = "任务管理中心")
public class TaskServiceApiImpl extends AuthcController {

    private final TaskApplicationService taskApplicationService;

    @RequestMapping(value = "/feign/task/addTask", method = RequestMethod.POST)
    @JsmileLog(title = "任务中心->新增任务", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.TASK_CENTER)
    public JSmileResult addTask(@RequestBody @Valid ReqTaskInsert reqTaskInsert, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TASK_CENTER);
        reqTaskInsert.setCurTenantId(jsmileContext.getTenantId());
        taskApplicationService.saveTask(reqTaskInsert);
        return JSmileResult.success();
    }

    @RequestMapping(value = "/feign/task/pauseJob", method = RequestMethod.POST)
    @JsmileLog(title = "任务中心->暂停任务", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.TASK_CENTER)
    public JSmileResult pauseJob(@RequestBody @Valid ReqBatchTaskDetail reqBatchTaskDetail, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TASK_CENTER);
        reqBatchTaskDetail.setCurTenantId(jsmileContext.getTenantId());
        taskApplicationService.pauseJob(reqBatchTaskDetail);
        return JSmileResult.success();
    }

    @RequestMapping(value = "/feign/task/resumeJob", method = RequestMethod.POST)
    @JsmileLog(title = "任务中心->恢复任务", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.TASK_CENTER)
    public JSmileResult resumeJob(@RequestBody @Valid ReqBatchTaskDetail reqBatchTaskDetail, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TASK_CENTER);
        reqBatchTaskDetail.setCurTenantId(jsmileContext.getTenantId());
        taskApplicationService.resumeJob(reqBatchTaskDetail);
        return JSmileResult.success();
    }

    @RequestMapping(value = "/feign/task/runJob", method = RequestMethod.POST)
    @JsmileLog(title = "任务中心->启动任务", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.TASK_CENTER)
    public JSmileResult runJob(@RequestBody @Valid ReqBatchTaskDetail reqBatchTaskDetail, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TASK_CENTER);
        reqBatchTaskDetail.setCurTenantId(jsmileContext.getTenantId());
        taskApplicationService.runJob(reqBatchTaskDetail);
        return JSmileResult.success();
    }

    @RequestMapping(value = "/feign/task/deleteTasks", method = RequestMethod.POST)
    @JsmileLog(title = "任务中心->删除任务", type = LogTypeEnum.DELETE, systemId = JSmileConstant.SystemId.TASK_CENTER)
    public JSmileResult deleteTasks(@RequestBody @Valid ReqTaskDelete reqTaskDelete, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TASK_CENTER);
        reqTaskDelete.setCurTenantId(jsmileContext.getTenantId());
        taskApplicationService.deleteTasks(reqTaskDelete);
        return JSmileResult.success();
    }

    @RequestMapping(value = "/feign/task/detail", method = RequestMethod.POST)
    @JsmileLog(title = "任务中心->任务详情", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.TASK_CENTER)
    public JSmileResult detail(@RequestBody @Valid ReqTaskDetail reqTaskDetail, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TASK_CENTER);
        reqTaskDetail.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(taskApplicationService.detail(reqTaskDetail));
    }

    @RequestMapping(value = "/feign/task/page", method = RequestMethod.POST)
    @JsmileLog(title = "任务中心->分页任务列表", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.TASK_CENTER)
    public JSmileResult page(@RequestBody @Valid ReqTaskQuery reqTaskQuery, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TASK_CENTER);
        reqTaskQuery.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(taskApplicationService.page(reqTaskQuery));
    }
}
